package com.sos.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import com.sos.widget.R;

public class RoundImageView extends androidx.appcompat.widget.AppCompatImageView {

    float width,height;
    private int leftTopRadius;
    private int rightTopRadius;
    private int leftBottomRadius;
    private int rightBottomRadius;
    private int defaultRadius=0;
    private int radius=0;

    public RoundImageView(Context context) {
        this(context, null);
    }
 
    public RoundImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        init(context, attrs);
    }
 
    public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
        if (Build.VERSION.SDK_INT < 18) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
    }
 
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width = getWidth();
        height = getHeight();
    }
 
    private void init(Context context,AttributeSet attrs){
        //读取自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
        radius = typedArray.getDimensionPixelOffset(R.styleable.RoundImageView_radius,defaultRadius);
        leftTopRadius = typedArray.getDimensionPixelOffset(R.styleable.RoundImageView_left_top_radius,defaultRadius);
        rightTopRadius = typedArray.getDimensionPixelOffset(R.styleable.RoundImageView_right_top_radius,defaultRadius);
        leftBottomRadius = typedArray.getDimensionPixelOffset(R.styleable.RoundImageView_left_bottom_radius,defaultRadius);
        rightBottomRadius = typedArray.getDimensionPixelOffset(R.styleable.RoundImageView_right_bottom_radius,defaultRadius);
 
        //如果四个角的值没有设置，就用通用的radius
        if(leftTopRadius == defaultRadius){
            leftTopRadius = radius;
        }
        if(rightTopRadius == defaultRadius){
            rightTopRadius = radius;
        }
        if(leftBottomRadius == defaultRadius){
            leftBottomRadius = radius;
        }
        if(rightBottomRadius == defaultRadius){
            rightBottomRadius = radius;
        }
        typedArray.recycle();
 
    }

    public void setRadius( int lt,
                           int rt,
                           int lb,
                           int rb){
        leftTopRadius = lt;
        rightTopRadius = rt;
        leftBottomRadius = lb;
        rightBottomRadius = rb;

        //如果四个角的值没有设置，就用通用的radius
        if(leftTopRadius == defaultRadius){
            leftTopRadius = radius;
        }
        if(rightTopRadius == defaultRadius){
            rightTopRadius = radius;
        }
        if(leftBottomRadius == defaultRadius){
            leftBottomRadius = radius;
        }
        if(rightBottomRadius == defaultRadius){
            rightBottomRadius = radius;
        }
        invalidate();
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
 
        if (width > 12 && height > 12) {
            Path path = new Path();
            //*向路径中添加圆角矩形。radii数组定义圆角矩形的四个圆角的x,y半径。radii长度必须为8*//*
            float rids[] = {leftTopRadius,leftTopRadius,rightTopRadius,rightTopRadius,rightBottomRadius,rightBottomRadius,leftBottomRadius,leftBottomRadius};
            path.addRoundRect(new RectF(0,0,width,height),rids, Path.Direction.CW);
            canvas.clipPath(path);
        }
 
        super.onDraw(canvas);
    }
}